package com.timothy.junk.common.db;

import com.timothy.junk.common.bean.JunkNotify;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class JunkNotifyDB {

	private static final String TAG = "JunkDB";

	//Database Name
	private static final String DATABASE_NAME = "db_junk";
	//Database Version
	private static final int DATABASE_VERSION = 1;
	//Table Name
	private static final String DATABASE_TABLE = "notify_tbl";

	/**
	 * Table columns
	 */
	public static final String ID = "ID";
	public static final String JUNK_NAME = "JUNK_NAME";
	public static final String START_HOUR = "START_HOUR";
	public static final String START_MINUTE = "START_MINUTE";
	public static final String END_HOUR = "END_HOUR";
	public static final String END_MINUTE = "END_MINUTE";
	

	/**
	 * Database creation sql statement
	 */
	private static final String CREATE_ANSWER_TABLE = "create table if not exists "
			+ DATABASE_TABLE + "(" + ID + " integer not null,"
			+ JUNK_NAME + " text not null,"
			+ START_HOUR + " integer not null," 
			+ START_MINUTE + " integer not null,"
			+ END_HOUR + " integer not null,"
			+ END_MINUTE + " integer not null,"
			+ "primary key("+ ID + ")" + ");";

	private final Context mCtx;
	private DatabaseHelper mDbHelper;
	private SQLiteDatabase mDb;

	/**
	 * Inner private class. Database Helper class for creating and updating database.
	 */
	private static class DatabaseHelper extends SQLiteOpenHelper {
		DatabaseHelper(Context context) {
			super(context, DATABASE_NAME, null, DATABASE_VERSION);
		}
		/**
		 * onCreate method is called for the 1st time when database doesn't exists.
		 */
		@Override
		public void onCreate(SQLiteDatabase db) {
			Log.i(TAG, "Creating DataBase: " + CREATE_ANSWER_TABLE);
			db.execSQL(CREATE_ANSWER_TABLE);
		}
		/**
		 * onUpgrade method is called when database version changes.
		 */
		@Override
		public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
			Log.w(TAG, "Upgrading database from version " + oldVersion + " to "+ newVersion);
		}
	}

	/**
	 * Constructor - takes the context to allow the database to be
	 * opened/created
	 *
	 * @param ctx the Context within which to work
	 */
	public JunkNotifyDB(Context ctx) {
		this.mCtx = ctx;
	}
	/**
	 * This method is used for creating/opening connection
	 * @return instance of DatabaseUtil
	 * @throws SQLException
	 */
	public JunkNotifyDB open() throws SQLException {
		mDbHelper = new DatabaseHelper(mCtx);
		mDb = mDbHelper.getWritableDatabase();
		return this;
	}
	
	public void close() {
		mDbHelper.close();
	}


	public Cursor getJunkNotify(long id) throws SQLException {
		String con = ID + "=" + id ;
		Cursor mCursor = mDb.query(true, DATABASE_TABLE, new String[] {ID,JUNK_NAME,START_HOUR,START_MINUTE,END_HOUR,END_MINUTE}, con, null,null, null, null, null);
		return mCursor;
	}
	
	public long createJunkNotify(JunkNotify jnBean) {
		ContentValues initialValues = new ContentValues();
		initialValues.put(ID, jnBean.getId());
		initialValues.put(JUNK_NAME, jnBean.getName());
		initialValues.put(START_HOUR, jnBean.getStartHour());
		initialValues.put(START_MINUTE, jnBean.getStartMinute());
		initialValues.put(END_HOUR, jnBean.getEndHour());
		initialValues.put(END_MINUTE, jnBean.getEndMinute());
		return mDb.insert(DATABASE_TABLE, null, initialValues);
	}
	
	public boolean updateJunkNotify(JunkNotify jnBean) {
		ContentValues args = new ContentValues();
		args.put(ID, jnBean.getId());
		args.put(JUNK_NAME, jnBean.getName());
		args.put(START_HOUR, jnBean.getStartHour());
		args.put(START_MINUTE, jnBean.getStartMinute());
		args.put(END_HOUR, jnBean.getEndHour());
		args.put(END_MINUTE, jnBean.getEndMinute());
		String con = ID + "=" + jnBean.getId();
		return mDb.update(DATABASE_TABLE,args,con, null) > 0;
	}
	
	public boolean deleteJunkNotify(int id) {
		return mDb.delete(DATABASE_TABLE, ID + "=" +id, null) > 0;
	}
	
	
	public boolean deleteAll() {
		return mDb.delete(DATABASE_TABLE, null, null) > 0;
	}

	public Cursor fetchAll() {
		return mDb.query(DATABASE_TABLE, new String[] {ID,JUNK_NAME,START_HOUR,START_MINUTE,END_HOUR,END_MINUTE}, null, null, null, null, null);
	}
	
	public int fetchCount() {
		int sum=0;
		Cursor cursor = mDb.query(DATABASE_TABLE, new String[] {ID,JUNK_NAME,START_HOUR,START_MINUTE,END_HOUR,END_MINUTE}, null, null, null, null, null);
		while(cursor.moveToNext()){
			sum++;
		}
		return sum;
	}
	
//	public void printStoredDataInDB(){
//		Log.i(TAG,"----------------data in SQLLite-----------------");
//    	Cursor cursor = fetchAllAnswers() ;
//    	while(cursor.moveToNext()){
//    		int cid = cursor.getInt(0);
//    		int qid = cursor.getInt(1);
//			String qidStr = cursor.getString(2);
//			String answer = cursor.getString(3);
//			Log.i(TAG, "cid="+String.valueOf(cid)+" qid="+String.valueOf(qid)+" qidStr="+qidStr+" answer="+answer);
//		}
//    	cursor.close();
//	}
}
